package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjNetworkInviteCodeSet;
import com.zb.mapper.ZjNetworkInviteCodeSetMapper;
import com.zb.service.ZjNetworkInviteCodeSetService;
import com.zb.vo.NetworkInviteCodeSetVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 网点邀请码设置表，加入企业邀请码 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-03-26
 */
@Service
public class ZjNetworkInviteCodeSetServiceImpl extends ServiceImpl<ZjNetworkInviteCodeSetMapper, ZjNetworkInviteCodeSet> implements ZjNetworkInviteCodeSetService {
    @Resource
    private ZjNetworkInviteCodeSetMapper networkInviteCodeSetMapper;

    // 初始化 或 恢复初始化网点邀请码设置
    @Override
    public void initOrRestoreDefaulNetworkSet(Long custAccId) {
        ZjNetworkInviteCodeSet inviteCodeSet = new ZjNetworkInviteCodeSet();
        inviteCodeSet.setId(custAccId);
        inviteCodeSet.setExpirtTime(1);
        inviteCodeSet.setEnableNotSuperManagerInvite(true);
        inviteCodeSet.setEnableInviteCheck(true);
        inviteCodeSet.setInviteType(1);
        inviteCodeSet.setInviteCode("");
        this.saveOrUpdate(inviteCodeSet);
    }

    // 获取服务商邀请码设置
    @Override
    public NetworkInviteCodeSetVo getNetworkInviteCodeSet(Long custAccId) {
        ZjNetworkInviteCodeSet codeSet = this.getById(custAccId);
        NetworkInviteCodeSetVo vo = new NetworkInviteCodeSetVo();
        if (null == codeSet) {
            vo.setExpirtTime(1);
            vo.setEnableNotSuperManagerInvite(true);
            vo.setEnableInviteCheck(true);
            vo.setInviteType(1);
            vo.setInviteCode("");
            return vo;
        }
        BeanUtil.copyProperties(codeSet, vo);
        return vo;
    }

    // 保存服务商邀请码设置
    @Override
    public void saveNetworkInviteCode(Long custAccId, NetworkInviteCodeSetVo inviteCodeSetVo) {
        Assert.isTrue(inviteCodeSetVo.getExpirtTime() >= 1, "邀请码有效期天数必须 >= 1 ");
        ZjNetworkInviteCodeSet inviteCodeSet = new ZjNetworkInviteCodeSet();
        inviteCodeSet.setId(custAccId);
        inviteCodeSet.setExpirtTime(inviteCodeSet.getExpirtTime());
        inviteCodeSet.setEnableNotSuperManagerInvite(inviteCodeSetVo.getEnableNotSuperManagerInvite());
        inviteCodeSet.setEnableInviteCheck(inviteCodeSetVo.getEnableInviteCheck());
        inviteCodeSet.setInviteType(inviteCodeSetVo.getInviteType());
        if (null != networkInviteCodeSetMapper.existNetworkInviteCodeSetById(custAccId)) {
            this.updateById(inviteCodeSet);
            return;
        }
        inviteCodeSet.setInviteCode("");
        this.save(inviteCodeSet);
    }

}
